package com.cskaoyan.mr.topn;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

/**
 * @ClassName TopN
 * @Description: TODO
 * @Author 远志 zhangsong@cskaoyan.onaliyun.com
 * @Date 2023/2/13 14:13
 * @Version V1.0
 **/
public class TopN {

    public static final int N = 3;

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //读取系统默认的配置文件信息
        Configuration configuration = new Configuration(true);
        //利用配置信息，实例化出一个jon实例，该job实例其实就是一个mr程序任务
        Job job = Job.getInstance(configuration);
        //建议将job设置一个名称，后续运行时，如果出错，比较容易排查
        job.setJobName("topn issue");
        job.setJarByClass(TopN.class);

        //设置输入、输出路径
        Path inPath = new Path(args[0]);
        TextInputFormat.addInputPath(job, inPath);
        Path outPath = new Path(args[1]);
        TextOutputFormat.setOutputPath(job, outPath);

        job.setMapperClass(TopNMapper.class);
        job.setMapOutputKeyClass(IntWritable.class);
        job.setMapOutputValueClass(NullWritable.class);

        job.setReducerClass(TopNReducer.class);

        job.waitForCompletion(true);
    }
}
